package com.houor.device.controller;

import com.houor.device.controller.handler.ControllerFrameHandler;
import com.houor.device.controller.handler.codec.ControllerFrameDecoder;
import com.houor.device.controller.handler.codec.ControllerFrameEncoder;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;

/**
 * Create configured {@link ChannelPipeline} for server-side channel.
 * 
 * @author Houor
 *
 * @createTime 2017-04-21 11:35
 */
public class DataServerInitializer extends ChannelInitializer<SocketChannel> {

	@Override
	protected void initChannel(SocketChannel ch) throws Exception {
		ChannelPipeline pipeline = ch.pipeline();

		// Add the encoder and decoder first,
		pipeline.addLast(new ControllerFrameEncoder());
		pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(2000, 0, 2, 0, 2));
		pipeline.addLast(new ControllerFrameDecoder());

		// and then business logic.
		// create a handler for every new channel
		// because it has stateful properties.
		pipeline.addLast(new ControllerFrameHandler());
		// pipeline.addLast(new MessageHandler());

	}

}
